package com.test.controller;

import com.test.bean.*;
import com.test.service.UserService;
import com.test.utils.ImportExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping(value = "party")
public class PartyController {
    @Autowired
    private UserService userService;

//    党员信息单条录入
    @ResponseBody
    @RequestMapping(value = "mem_add",method = RequestMethod.POST)
    public Result AddMem(Party_member party_member){
        Result result = new Result();
        try {
            userService.addPartymember(party_member);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    党员信息批量录入
    @ResponseBody
    @RequestMapping(value = "mem_adds",method = RequestMethod.POST)
    public Result form(HttpServletRequest request, HttpServletResponse response) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        Result result = new Result();
        InputStream in = null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");

        if (file.isEmpty()) {
            throw new Exception("文件不存在！");
        }
        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
        in.close();

        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            Party_member party_member = new Party_member();
            party_member.setBranch(String.valueOf(lo.get(0)));
            party_member.setId(Integer.parseInt(String.valueOf(lo.get(1))));
            party_member.setName(String.valueOf(lo.get(2)));
            party_member.setDuties(String.valueOf(lo.get(3)));
            party_member.setOutlook(String.valueOf(lo.get(4)));;
            party_member.setGender(String.valueOf(lo.get(5)));
            party_member.setBirth(String.valueOf(lo.get(6)));
            party_member.setNative_place(String.valueOf(lo.get(7)));;
            party_member.setPhone(String.valueOf(lo.get(8)));
            party_member.setMailbox(String.valueOf(lo.get(9)));
            party_member.setLive_place(String.valueOf(lo.get(10)));

            userService.addPartymember(party_member);
        }

        result.setCode(200);
        result.setMessage("excel解析录入成功");
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
        out = response.getWriter();
//            out.print("文件导入成功！");
        out.flush();
        out.close();
        return result;
    }

//  党员信息删除
    @ResponseBody
    @RequestMapping(value = "mem_delete",method = RequestMethod.DELETE)
    public Result Deletepartymem(int id){
        Result result = new Result();
        db_Party_member dparty_member = userService.queryPartymem(id);
        if(dparty_member != null){
            try {
                userService.deletePartymem(id);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    党支部单条录入
    @ResponseBody
    @RequestMapping(value = "branch_add",method = RequestMethod.POST)
    public Result AddMem(Party_branch party_branch){
        Result result = new Result();
        try {
            userService.addPartybranch(party_branch);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    党支部信息批量录入
    @ResponseBody
    @RequestMapping(value = "branch_adds",method = RequestMethod.POST)
    public Result formbranch(HttpServletRequest request, HttpServletResponse response) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        Result result = new Result();
        InputStream in = null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");

        if (file.isEmpty()) {
            throw new Exception("文件不存在！");
        }
        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
        in.close();

        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            Party_branch party_branch = new Party_branch();
            party_branch.setBranch_name(String.valueOf(lo.get(0)));
            party_branch.setClerk(String.valueOf(lo.get(01)));
            party_branch.setMember(Integer.parseInt(String.valueOf(lo.get(2))));
            party_branch.setFull_member(Integer.parseInt(String.valueOf(lo.get(3))));
            party_branch.setP_member(Integer.parseInt(String.valueOf(lo.get(4))));
            party_branch.setBranch_member(String.valueOf(lo.get(5)));
            party_branch.setTime(String.valueOf(lo.get(6)));
            userService.addPartybranch(party_branch);
        }

        result.setCode(200);
        result.setMessage("excel解析录入成功");
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
        out = response.getWriter();
    //out.print("文件导入成功！");
        out.flush();
        out.close();
        return result;
    }

    //党支部删除
    @ResponseBody
    @RequestMapping(value = "branch_delete",method = RequestMethod.DELETE)
    public Result DeletePartybranch(String branch_name){
        Result result = new Result();
        Party_branch_db party_branch_db = userService.queryPartybranch(branch_name);
        if(party_branch_db != null){
            try {
                userService.deletePartybranch(branch_name);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }

    //党员信息更新
    @ResponseBody
    @RequestMapping(value = "mem_update",method = RequestMethod.PUT)
    public Result ChangePartymem(int id,Party_member party_member){
        Result result = new Result();
        db_Party_member dparty_member = userService.queryPartymem(id);
        if(dparty_member != null){
            try {
                userService.updatePartymem(party_member);
                result.setMessage("修改成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();//在日志中输出错误
                result.setMessage("修改失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("修改失败");
            result.setCode(400);//异常
        }
        return result;
    }

    //党支部信息更新
    @ResponseBody
    @RequestMapping(value = "branch_update",method = RequestMethod.PUT)
    public Result ChangePartybranch(String branch_name,Party_branch party_branch){
        Result result = new Result();
        Party_branch_db party_branch_db = userService.queryPartybranch(branch_name);
        if(party_branch_db != null){
            try {
                userService.updatePartybranch(party_branch);
                result.setMessage("修改成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();//在日志中输出错误
                result.setMessage("修改失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("修改失败");
            result.setCode(400);//异常
        }
        return result;
    }

    //入党事件录入
    @ResponseBody
    @RequestMapping(value = "issue_add",method = RequestMethod.POST)
    public Result AddIssue( Party_issue party_issue){
        Result result = new Result();
        try {
            userService.addPartyissue(party_issue);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

    //入党事件查询
}
